package me.itsoo.artemis.framework.core.converter;

import lombok.extern.slf4j.Slf4j;

/**
 * BooleanTypeConvertor
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/18
 */
@Slf4j
public class BooleanTypeConvertor implements TypeConvertor {

    @Override
    public Object convertIfNecessary(Object value) {
        if (value == null) {
            return null;
        }

        if (value instanceof Boolean) {
            return value;
        }

        if (value instanceof Number) {
            final byte val = ((Number) value).byteValue();

            if (val == 0) {
                return false;
            }

            if (val == 1) {
                return true;
            }
        }

        return value.getClass().isPrimitive() ? false : null;
    }
}
